
/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2022原作者。
 *
 *根据Apache许可证2.0版许可（“许可证”）；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本：
 *
 *https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证进行的分发是在“按原样”的基础上进行的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证和
 *许可证下的限制。
 */

package org.springframework.validation.beanvalidation;

import java.lang.annotation.Annotation;
import java.util.function.Supplier;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.aopalliance.aop.Advice;

import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.Assert;
import org.springframework.util.function.SingletonSupplier;
import org.springframework.validation.annotation.Validated;

/**
 * A convenient {@link BeanPostProcessor} implementation that delegates to a
 * JSR-303 provider for performing method-level validation on annotated methods.
 *
 * <p>Applicable methods have JSR-303 constraint annotations on their parameters
 * and/or on their return value (in the latter case specified at the method level,
 * typically as inline annotation), e.g.:
 *
 * <pre class="code">
 * public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)
 * </pre>
 *
 * <p>Target classes with such annotated methods need to be annotated with Spring's
 * {@link Validated} annotation at the type level, for their methods to be searched for
 * inline constraint annotations. Validation groups can be specified through {@code @Validated}
 * as well. By default, JSR-303 will validate against its default group only.
 *
 * <p>As of Spring 5.0, this functionality requires a Bean Validation 1.1+ provider.
 *
 * @author Juergen Hoeller
 * @since 3.1
 * @see MethodValidationInterceptor
 * @see jakarta.validation.executable.ExecutableValidator
 */
/**
 *一个方便的{@linkBeanPostProcessor}实现，它将委托给
 *JSR-303提供程序，用于对带注释的方法执行方法级验证。
 *
 *<p>适用的方法在其参数上有JSR-303约束注释
 *和/或取决于它们的返回值（在后一种情况下，在方法级别指定，
 *通常作为内联注释），例如：
 *
 *<pre-class=“code”>
 *public@NotNull Object myValidMethod（@NotNull字符串arg1，@Max（10）int arg2）
 *</pre>
 *
 *<p>具有此类带注释方法的目标类需要使用Spring的
 *｛@link Validated｝类型级别的注释，以便搜索它们的方法
 *内联约束注释。验证组可以通过｛@code@Validated｝指定
 *同样。默认情况下，JSR-303将仅针对其默认组进行验证。
 *
 *<p>从Spring 5.0开始，此功能需要一个Bean Validation 1.1+提供程序。
 *
 *@作者于尔根·霍勒
 *@自3.1起
 *@见MethodValidationInterceptor
 *@参见jakarta.validation.executable.ExecutableValidator
 */
@SuppressWarnings("serial")
public class MethodValidationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor
		implements InitializingBean {

	private Class<? extends Annotation> validatedAnnotationType = Validated.class;

	private Supplier<Validator> validator = SingletonSupplier.of(() ->
			Validation.buildDefaultValidatorFactory().getValidator());


	/**
	 * Set the 'validated' annotation type.
	 * The default validated annotation type is the {@link Validated} annotation.
	 * <p>This setter property exists so that developers can provide their own
	 * (non-Spring-specific) annotation type to indicate that a class is supposed
	 * to be validated in the sense of applying method validation.
	 * @param validatedAnnotationType the desired annotation type
	 */
	/**
	 *设置“已验证”的批注类型。
	 *默认的已验证批注类型是｛@link validated｝批注。
	 *<p>此setter属性的存在使开发人员可以提供自己的
	 *（非Spring特定的）注释类型，以指示类
	 *在应用方法验证的意义上进行验证。
	 *@param validatedAnnotationType所需的注释类型
	 */
	public void setValidatedAnnotationType(Class<? extends Annotation> validatedAnnotationType) {
		Assert.notNull(validatedAnnotationType, "'validatedAnnotationType' must not be null");
		this.validatedAnnotationType = validatedAnnotationType;
	}

	/**
	 * Set the JSR-303 ValidatorFactory to delegate to for validating methods,
	 * using its default Validator.
	 * <p>Default is the default ValidatorFactory's default Validator.
	 * @see jakarta.validation.ValidatorFactory#getValidator()
	 */
	/**
	 *将JSR-303 ValidatorFactory设置为委托给验证方法，
	 *使用其默认Validator。
	 *＜p＞Default是默认ValidatorFactory的默认Validator。
	 *@参见jakarta.validation.ValidatorFactory#getValidator（）
	 */
	public void setValidatorFactory(ValidatorFactory validatorFactory) {
		this.validator = SingletonSupplier.of(validatorFactory::getValidator);
	}

	/**
	 * Set the JSR-303 Validator to delegate to for validating methods.
	 * <p>Default is the default ValidatorFactory's default Validator.
	 */
	/**
	 *将JSR-303 Validator设置为委托给以验证方法。
	 *＜p＞Default是默认ValidatorFactory的默认Validator。
	 */
	public void setValidator(Validator validator) {
		this.validator = () -> validator;
	}

	/**
	 * Set a lazily initialized Validator to delegate to for validating methods.
	 * @since 6.0
	 * @see #setValidator
	 */
	/**
	 *将延迟初始化的Validator设置为委托给以验证方法。
	 *@自6.0起
	 *@参见#setValidator
	 */
	public void setValidatorProvider(ObjectProvider<Validator> validatorProvider) {
		this.validator = validatorProvider::getObject;
	}


	@Override
	public void afterPropertiesSet() {
		Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true);
		this.advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(this.validator));
	}

	/**
	 * Create AOP advice for method validation purposes, to be applied
	 * with a pointcut for the specified 'validated' annotation.
	 * @param validator a Supplier for the Validator to use
	 * @return the interceptor to use (typically, but not necessarily,
	 * a {@link MethodValidationInterceptor} or subclass thereof)
	 * @since 6.0
	 */
	/**
	 *为方法验证目的创建AOP建议，以便应用
	 *带有指定的“validated”注释的切入点。
	 *@param validator验证程序要使用的供应商
	 *@返回要使用的拦截器（通常但不一定，
	 *｛@link MethodValidationInterceptor｝或其子类）
	 *@自6.0起
	 */
	protected Advice createMethodValidationAdvice(Supplier<Validator> validator) {
		return new MethodValidationInterceptor(validator);
	}

}
